import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutterdemo/common/config/global.dart';
import 'package:flutterdemo/common/data/Repository/UserRepository.dart';
import 'package:flutterdemo/common/data/models/request/LoginReq.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:table_calendar/table_calendar.dart';

import 'common/base/BaseStatefulWidget.dart';
import 'common/base/BaseStatelessWidget.dart';
import 'common/data/models/response/UserModel.dart';
import 'common/data/net2/address.dart';
import 'common/data/net2/http_manager.dart';
import 'common/data/net2/result_data.dart';
import 'common/data/net3/DioManager.dart';

void main() {
  Global.init(() {
    runApp(MyApp());
  });
}

///继承自BaseStatefulWidget
class MyApp extends BaseStatelessWidget {
  ///initDefaultBuild
  @override
  Widget initDefaultBuild(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(750, 1334),
      builder: () => MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Flutter_ScreenUtil',
        theme: ThemeData(
          primarySwatch: Colors.blue,
          //要支持下面这个需要使用第一种初始化方式
          textTheme: TextTheme(button: TextStyle(fontSize: 45.sp)),
        ),
        builder: (context, widget) {
          return MediaQuery(
            ///设置文字大小不随系统设置改变
            data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
            child: widget,
          );
        },
        home: MyHome(),
      ),
    );
  }
}

///继承自BaseStatefulWidget
class MyHome extends BaseStatefulWidget {
  final String title = "首页";
  @override
  BaseStatefulState<BaseStatefulWidget> getState() => _MyHomeState();
}

class _MyHomeState extends BaseStatefulState<MyHome> {
  int _counter = 0;
  @override
  void initDefaultState() {
    //companyID=6976104584143916066&version=6.9.6&customID=6976104584143916066
  }

  Future<void> _incrementCounter() async {
    setState(() {
      _counter++;
    });

    // var map = new Map<String, dynamic>();
    // ResultData res =
    //     HttpManager.getInstance().get(Address.TEST_API, params: map);
    // var status = res.isSuccess ? "1" : "0";
    // print('login=' + status + ',data=' + res.data);

    // getWebSynch();
    // getWebSynch1();
    getWebSynch2();
  }

  void getWebSynch2() {
    // companyID=6976104584143916066&version=6.9.6&customID=6976104584143916066
    LoginReq req =
        LoginReq("6976104584143916066", "6.9.6", "6976104584143916066");
    UserRepository.login(req);
  }

  void getWebSynch1() {
    print("getWebSynch1======");
    var map = new Map<String, String>();
    map["customID"] = "6976104584143916066";
    map["version"] = "6.9.4";
    map["companyID"] = "6976104584143916066";
    var headers = new Map<String, String>();
    DioManger.getInstance().get(Address.TEST_API, map, headers,
        (data, CancelToken token) {
      print("========获取了数据" + token.toString());
    }, (error) {
      print(error);
    });
  }

  void getWebSynch() async {
    var map = new Map<String, String>();
    map["customID"] = "6976104584143916066";
    map["version"] = "6.9.4";
    map["companyID"] = "6976104584143916066";
    dynamic res =
        await HttpManager.getInstance().get(Address.TEST_API, params: map);
    print('res=' + res["mobileHost"]);
  }

  @override
  Widget initDefaultBuild(BuildContext context) {
    final TextStyle headline1 = Theme.of(context).textTheme.headline4;
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the buaaatton this many times:',
              style: GoogleFonts.oswald(textStyle: headline1),
            ),
            Text(
              '$_counter',
              style: GoogleFonts.lato(fontStyle: FontStyle.italic),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  @override
  void initDefaultDispose() {}
}
